//
//  HZNetworkTools.swift
//  HansWeibo
//
//  Created by Hans on 16/8/11.
//  Copyright © 2016年 Hans. All rights reserved.
//

import UIKit
import AFNetworking
// 网络请求枚举
enum HZNetworkToolsENUM: String {
    // GET 请求
    case GET = "GET"
    // POST 请求
    case POST = "POST"
    
}

/*
    对AFN进行封装
    提供一个可以发送GET和POST请求的公共方法
    帮助当前app 请求所有接口数据
 */

class HZNetworkTools: AFHTTPSessionManager {
    // 全局的访问点  单例
    static let sharedTools:HZNetworkTools = {
        let tools = HZNetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    
    
    func requset(method: HZNetworkToolsENUM, urlString: String, parameters:AnyObject?, success:(response: AnyObject?)->(), failure: (error: NSError)->()) {
        
        if method == .GET {
            // GET 请求
            GET(urlString, parameters: parameters, progress: nil, success: { (_, response) in
                success(response: response)
                }, failure: { (_, error) in
                    failure(error: error)
            })
        }else {
            // POST请求
            POST(urlString, parameters: parameters, progress: nil, success: { (_, response) in
                success(response: response)
                }, failure: { (_, error) in
                    failure(error: error)
            })
        }
        
    }
    
}

//MARK: - 发布微博
extension HZNetworkTools {
    // 文字微博
    func composeUpdata(status:String, success:(response: AnyObject?)->(), failure: (error: NSError)->()){
        // 请求的url
        let urlString = "https://api.weibo.com/2/statuses/update.json"
        // 请求参数
        let params = ["access_token":HZUserAccountViewModel.sharedTools.access_token!,
                      "status":status]
        requset(HZNetworkToolsENUM.POST, urlString: urlString, parameters: params, success: success, failure: failure)
    }
    
    
    func composeUpload(status:String ,images:[UIImage],success:(response: AnyObject?)->(), failure: (error: NSError)->()){
        // 请求的url
        let urlString = "https://upload.api.weibo.com/2/statuses/upload.json"
        // 请求参数
        let parmas = ["access_token":HZUserAccountViewModel.sharedTools.access_token!,
                      "status":status]
        // 发送请求
        POST(urlString, parameters: parmas, constructingBodyWithBlock: { (formdata) in
            // 二进制
            let data = UIImagePNGRepresentation(images.first!)!
            formdata.appendPartWithFileData(data, name: "pic", fileName: "dad", mimeType: "application/octet-stream")
            }, progress: nil, success: { (_, response) in
                success(response: response)
            }) { (_, error) in
                failure(error: error)
        }
        
    }
    
}

//MARK: - 微博首页数据
extension HZNetworkTools {
    
    func homeLoadData( since_id: Int64, max_id: Int64, success:(response: AnyObject?)->(), failure: (error: NSError)->()){
    //请求的urlString
    let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
    //请求参数
    let params = ["access_token": HZUserAccountViewModel.sharedTools.userAccount!.access_token!,"since_id":"\(since_id)" , "max_id": "\(max_id)"]
        
        
        requset(HZNetworkToolsENUM.GET, urlString: urlString, parameters: params, success: success, failure: failure)
    }
}



//MARK: - 授权界面接口
extension HZNetworkTools {
    
    // 请求授权
    func oauthLoadUserAccount(code: String, success:(response: AnyObject?)->(), failure: (error: NSError)->()){
        
        //请求的url
        let urlString = "https://api.weibo.com/oauth2/access_token"
        //请求参数
        let params = ["client_id":WBAppKey,
                      "client_secret":WBAppSecret,
                      "grant_type":"authorization_code",
                      "code":code,
                      "redirect_uri":WBRedirect_uri]
        
        requset(HZNetworkToolsENUM.POST, urlString: urlString, parameters: params, success: success, failure: failure)
        
        
    }
    
    //请求个人数据
    func oauthLoadUserInfo(userAccount: HZUserAccessModel, success:(response: AnyObject?)->(), failure:(error: NSError)->()){
        //请求的url
        let urlString = "https://api.weibo.com/2/users/show.json"
        //请求参数
        let params = ["access_token":userAccount.access_token!,
                      "uid":userAccount.uid!]
        
        requset(HZNetworkToolsENUM.GET, urlString: urlString, parameters: params, success: success, failure: failure)
    }
    
    
    
    
    
    
}




//MARK: - 请求天气接口
extension HZNetworkTools {
    func getWeatherData(success:(response: AnyObject?)->(), failure:(error: NSError)->()){
        let urlString = "http://www.weather.com.cn/data/sk/101010100.html"
        
        requset(HZNetworkToolsENUM.GET, urlString: urlString, parameters: nil, success: success, failure: failure)
    }
}









